package TUILayer;

import java.util.Scanner;
import java.util.ArrayList;

import ControlLayer.*;
import ModelLayer.*;


/**
 * User interface class for the loan part of the application.
 * 
 * @author Sebastian K Munch
 * @version 08-11-2012
 */
public class LoanUI
{
    LoanCtr loanCtr;
    DVDCtr dvdCtr;
    AddressCtr aCtr;
    /**
     * Constructor for objects of class LoanUI
     */
    public LoanUI()
    {
        loanCtr = new LoanCtr();
        dvdCtr = new DVDCtr();
        aCtr = new AddressCtr();
    }
    
    public void start()
    {
        clscr();
        loanMenu();
    }
    
    private void loanMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeMenu();
            switch(choise)
            {
                case 1:
                    showLoan();
                    break;
                case 2:
                    createLoan();
                    break;
                case 3:
                    returnDvd();
                    break;
                case 4:
                    showLoanList();
                    break;
                case 5:
                    exit = true;
                    break;
                default:
                    invalidChoise();
                    break;
            }
        }
    }
    
    private int writeMenu()
    {
        print("");
        print("");
        print(" *** Loan Menu *** ");
        print(" (1) Show loan information");
        print(" (2) Create new loan");
        print(" (3) Return DVD");
        print(" (4) Show current loan list");
        print(" (5) Return");
        int choise = 0;
        while(choise == 0)
        {
            try
            {
                 Scanner keyboard = new Scanner(System.in);
                 System.out.print("\n Make your choise: ");
                 choise = keyboard.nextInt();
            }
            catch(Exception e)
            {
                 System.out.println("Invalid input, try again.");
            }
        }
        return choise;
    }
    
    private long inputLong(String str)
    {
        long input = 0;
        while(input == 0)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                System.out.println(str);
                input = keyboard.nextLong();
            }
            catch(Exception e)
            {
                System.out.println("Invalid input, try again.");
            }
        }
        return input;
    }
    
    private String inputLine(String str)
    {
        String input = "";
        while(input.equals(""))
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                System.out.println(str);
                input = keyboard.nextLine();
            }
            catch(Exception e)
            {
                System.out.println("Invalid input, try again.");
            }
        }
        return input;
    }
    
    private void showLoanDetails(Loan loan)
    {
        print("Loan ID: " + loan.getLoanID());
        print("Loan holder: " + loan.getPerson().getFirstName() + " " + loan.getPerson().getSurName());
        print("Loan holder ID: " + loan.getPerson().getId());
        print("Loaned DVD title: " + loan.getDvd().getTitle());
        print("Loaned DVD ID: " + loan.getDvd().getID());
        print("Loaned copy serial number: " + loan.getCopy().getSerialNumber());
        print("Loan made on date: " + loan.getLoanMadeDate());
        print("Loan status: " + loan.getStatusText());
        switch(loan.getStatus())
        {
            case Loan.ACTIVE:
                print("DVD is to be returned on date: " + loan.getToBeReturned());
                break;
            case Loan.RETURNED:
                print("DVD was returned on date: " + loan.getReturned());
                break;
        }
    }
    
    private void showDvdList(ArrayList<DVD> list)
    {
        for(DVD d : list)
        {
            print("ID: " + d.getID() + " Title: " + d.getTitle());
        }
    }
    
    private void showCopyList(ArrayList<Copy> list)
    {
        for(Copy c : list)
        {
            print("Serial Number: " + c.getSerialNumber());
        }
    }
    
    private void showPersonList(ArrayList<Person> persons)
    {
        for(Person p : persons)
        {
            print("ID: " + p.getId() + " Name: " + p.getFirstName() + " " + p.getSurName());
        }
    }
    
    private void showLoan()
    {
        long id = inputLong("Type the loan id number: ");
        Loan l;
        clscr();
        try
        {
            l = loanCtr.findLoan(id);
            showLoanDetails(l);
        }
        catch(Exception e)
        {
            print("Could not find loan.");
        }
    }
    
    private void createLoan()
    {
        boolean exit = false;
        long dvdID = 0;
        String serial = "";
        long personID = 0;
        if(!dvdCtr.isDvdListEmpty() && !exit)
        {
            showDvdList(dvdCtr.listAllDvds());
            dvdID = inputLong("Type the ID of the dvd you wish to loan: ");
        }
        else
        {
            clscr();
            print("There are no DVDs in the collection.");
            exit = true;
        }
        if(!exit)
        if(dvdCtr.findDVD(dvdID).listAllCopies().size() > 0)
        {
            showCopyList(dvdCtr.findDVD(dvdID).listAllCopies());
            serial = inputLine("Type the copy serial number: ");
        }
        else
        {
            clscr();
            print("There are no copies of this DVD.");
            exit = true;
        }
        if(!exit)
        if(aCtr.listAllPersons().size() > 0)
        {
            showPersonList(aCtr.listAllPersons());
            personID = inputLong("Type the id of the loaner: ");
        }
        else
        {
            clscr();
            print("There are no persons in the address book.");
            exit = true;
        }
        if(!exit)
        {
            String made = inputLine("Type the date on witch this loan was made: ");
            String retur = inputLine("Type the date on witch this dvd is to be returned: ");
        
            DVD d = dvdCtr.findDVD(dvdID);
            Copy c = d.findCopy(serial);
            Person p = aCtr.getPerson(personID);
            
            loanCtr.createLoan(d,c,p,made,retur);
            
            clscr();
            print("A new loan was made.");
        }
    }
    
    private void returnDvd()
    {
        long loanID = inputLong("Type the loan id: ");
        
        clscr();
        try
        {
            loanCtr.findLoan(loanID).setStatus(Loan.RETURNED);
            print("A dvd was returned.");
        }
        catch(Exception e)
        {
            print("Could not find loan.");
        }
    }
    
    private void showLoanList()
    {
        ArrayList<Loan> list = loanCtr.listAllLoans();
        clscr();
        for(Loan l : list)
        {
            print("");
            showLoanDetails(l);
        }
    }
    
    private void invalidChoise(){}
    
    private void print(String str)
    {
        System.out.println(str);
    }
    
    private void clscr()
    {
        print("\f");
    }
}
